package leetcode;

import java.util.ArrayList;
import java.util.List;

public class a257二叉树的所有路径 {
    public List<String> binaryTreePaths(TreeNode root) {
        List<Integer> path = new ArrayList<Integer>();
        List<String> res = new ArrayList<String>();

        if (root==null){
            return res;
        }
        traversal(root,path,res);
        return res;

    }

    public void traversal(TreeNode root,List<Integer> path,List<String> res){
//        中左右
        path.add(root.val);
        if (root.left==null&&root.right==null){
            StringBuilder sb=new StringBuilder();
            for (int i = 0; i <path.size()-1 ; i++) {
                sb.append(path.get(i)).append("->");//例如[4,5,6]变成4->5->
            }
            sb.append(path.get(path.size()-1));
            res.add(sb.toString());

        }
        if (root.left!=null){
            traversal(root.left,path,res);
            path.remove(path.size()-1);
        }if (root.right!=null){
            traversal(root.right,path,res);
            path.remove(path.size()-1);
        }
    }
}
